package com.zry.retorfit.retrofit;

import android.util.Log;


import com.zry.retorfit.config.RetrofitApplication;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * @创建者 ray@wdzj.com
 * @描述 配置Retrofit（配置网络缓存cache、配置持久cookie免登录）
 */

public class BaseApiRetrofit {

    public static String BASE_URL = "http://cs.sxjagf.com/";

    private HttpLoggingInterceptor loggingInterceptor =
            new HttpLoggingInterceptor(message -> Log.i("RetrofitLog",
                    "retrofitBack=$message")).setLevel(HttpLoggingInterceptor.Level.BODY);

    public OkHttpClient okHttpClient =
            new OkHttpClient.Builder().cookieJar(RetrofitApplication.getInstance().cookieJar)
                    .addInterceptor(loggingInterceptor)
                    .connectTimeout(15, TimeUnit.SECONDS)
                    .readTimeout(20, TimeUnit.SECONDS)
                    .writeTimeout(20, TimeUnit.SECONDS)
                    .retryOnConnectionFailure(true).build();

    public ApiDynamic retrofit =
            new Retrofit.Builder()
                    // 设置 网络请求 Url
                    .baseUrl(BASE_URL)
                    //设置使用Gson解析(记得加入依赖)
                    .addConverterFactory(new NullOnEmptyConverterFactory())
                    .addConverterFactory(GsonConverterFactory.create())
                    .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                    .client(okHttpClient)
                    .build().create(ApiDynamic.class);

}
